package com.example.gybicycle.controller.web;
import  com.alibaba.fastjson.JSON;

import com.example.gybicycle.entity.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.example.gybicycle.common.Log.SysLoging;
import com.example.gybicycle.common.entity.NormalPage;
import com.example.gybicycle.common.entity.Result;
import com.example.gybicycle.common.util.*;
import com.example.gybicycle.entity.SysUser;
import com.example.gybicycle.service.SysUserService;



@Tag(name = "用户")
@RestController
@RequestMapping("/web/sysUser")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;
    @PostMapping("/get")
    @SysLoging("用户管理-查看")
    @Operation(summary = "根据id查看")
    @RequiresPermissions("user:list")
    @ApiOperationSupport(includeParameters = {"sysUser.id"})
    public Result get(@RequestBody SysUser sysUser){
      sysUser=sysUserService.get(sysUser.getId());
        return Result.success(sysUser);
    }
    @PostMapping("/list")
    @Operation(summary =  "列表查看")
    @ApiOperationSupport(includeParameters = {"page.id","page.page","page.size","page.name","sysUser.state"})
    @RequiresPermissions("user:list")
    @SysLoging("用户管理-查看")
    public Result findList(@RequestBody(required=false) SysUser page){
       
        
       
        Page<SysUser> list = PageHelper.startPage(page.getPage(), page.getSize()).doSelectPage(
                () ->sysUserService.findList(page));
        return Result.success(NormalPage.createPage(list));
    }
    @PostMapping("/insert")
    @Operation(summary =  "添加")
    @RequiresPermissions("user:insert")
    @SysLoging("用户管理-添加")
    public Result insert(@RequestBody SysUser sysUser, HttpServletRequest request){
         LoginInfo loginInfo=LoginUtil.getLoginInfo(request);
        if(loginInfo==null)
            return Result.loginOut();
        sysUser.setEditBy(loginInfo.getUser().getId());
        sysUser.setEditTime(new Date());
        int num=sysUserService.insert(sysUser);
        return Result.reData(num,sysUser);
    }
    @PostMapping("/update")
    @Operation(summary =  "修改")
    @RequiresPermissions("user:update")
    @SysLoging("用户管理-修改")
    public Result update(@RequestBody SysUser sysUser, HttpServletRequest request){
        LoginInfo loginInfo=LoginUtil.getLoginInfo(request);
        if(loginInfo==null)
            return Result.loginOut();
        sysUser.setEditBy(loginInfo.getUser().getId());
        sysUser.setEditTime(new Date());
        int num=sysUserService.update(sysUser);
        return Result.reData(num,sysUser);
    }
  @PostMapping("/delete")
    @Operation(summary =  "删除")
    @RequiresPermissions("user:delete")
    @SysLoging("用户管理-删除")
    public Result delete(@RequestBody SysUser entity){
        int num=sysUserService.delete(entity.getId());
        if(num>0)
            return Result.success("删除成功");
        else 
            return Result.fail("删除失败");
    }
     @PostMapping("deletelist")
    @Operation(summary =  "批量删除")
    @ApiOperationSupport(includeParameters = {"sysUser.id"})
    @RequiresPermissions("user:delete")
    @SysLoging("用户管理-批量删除")
    public Result deletelist(@RequestBody List<String> data){
        int num=sysUserService.deleteByBatch(data);
         if(num>0)
             return Result.success("删除成功");
         else
             return Result.fail("删除失败"); 
    }
}